package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PropertyInfo;
import com.ruoyi.system.mapper.PropertyInfoMapper;
import com.ruoyi.system.service.IPropertyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class PropertyInfoServiceImpl implements IPropertyInfoService {

    @Autowired
    private PropertyInfoMapper propertyInfoMapper;

    @Override
    public List<PropertyInfo> selectPropertyInfoList(PropertyInfo propertyInfo) {
        return propertyInfoMapper.selectPropertyInfoList(propertyInfo);
    }

    @Override
    public int insertPropertyInfo(PropertyInfo propertyInfo) {
        String infoId = UUID.randomUUID().toString().replaceAll("-", "");
        propertyInfo.setInfoId(infoId);
        return propertyInfoMapper.insertPropertyInfo(propertyInfo);
    }

    @Override
    public PropertyInfo getPropertyInfoById(String infoId) {
        return propertyInfoMapper.getPropertyInfoById(infoId);
    }

    @Override
    public int updatePropertyInfo(PropertyInfo propertyInfo) {
        return propertyInfoMapper.updatePropertyInfo(propertyInfo);
    }

    @Override
    public int removePropertyInfo(String ids) {
        return propertyInfoMapper.removePropertyInfo(Convert.toStrArray(ids));
    }

    @Override
    public String importPropertyInfo(List<PropertyInfo> piList) {
        //校验
        int errorData = 0;
        int updateCount = 0;
        int insertCount = 0;
        StringBuilder errorDatas = new StringBuilder();
        for (PropertyInfo pi : piList) {
            if (null == pi.getNum()) {
                errorData++;
                String errStr = "人员信息" + pi.getName() + "序号为空；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if (!"1".equals(pi.getStatus()) && !"2".equals(pi.getStatus()) && !"3".equals(pi.getStatus()) && !"4".equals(pi.getStatus())) {
                String errStr = "人员信息" + pi.getName() + "状态异常；</br>";
                errorDatas.append(errStr);
                continue;
            }
            if (StringUtils.isNotEmpty(pi.getInfoId())) { //update
                try {
                    PropertyInfo propertyInfo = propertyInfoMapper.getPropertyInfoById(pi.getInfoId());
                    if (propertyInfo != null) {
                        propertyInfoMapper.updatePropertyInfo(pi);
                    } else {
                        propertyInfoMapper.insertPropertyInfo(pi);
                    }
                    updateCount++;
                } catch (Exception e) {
                    e.printStackTrace();
                    errorData++;
                    String errStr = "人员信息" + pi.getName() + "修改失败；" + e.getMessage() + "</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            } else { //insert
                try {
                    pi.setInfoId(UUID.randomUUID().toString().replaceAll("-", ""));
                    propertyInfoMapper.insertPropertyInfo(pi);
                    insertCount++;
                } catch (Exception e) {
                    e.printStackTrace();
                    errorData++;
                    String errStr = "人员信息" + pi.getName() + "新增失败；" + e.getMessage() + "</br>";
                    errorDatas.append(errStr);
                    continue;
                }
            }
        }
        String message;
        if (piList.size() > 0) {
            message = "共导入" + piList.size() + "条数据。</br>";
        } else {
            return "无数据";
        }
        message = message + "成功新增" + insertCount + "条数据，成功更新" + updateCount + "条数据</br>";
        if (StringUtils.isNotEmpty(errorDatas.toString())) {
            message = message + "其中" + errorData + "条数据导入失败，错误原因如下：</br>";
            message = message + errorDatas.toString();
        }
        return message;
    }
}